﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OrderSystem;
using OrderSystem.Models;
//using DomainModel.ClassesRepository;
using OrderSystem.Entity;


namespace AdministrationMVC.Controllers
{
    public class AdministratorController : Controller
    {
        private OrderSystem.Entity.OrderSystemDBEntities db
        {
            get { return OrderSystem.UserManager.Context; }
        }

        //
        // GET: /User/

        [Authorize]
        public ActionResult Index()
        {
            if (! UserManager.IsInRole(User.Identity.Name, "Administrator"))
                return HttpNotFound();
            return View(db.UsersSets.ToList());
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id = 0)
        {
            UsersSet usermodel = db.UsersSets.FirstOrDefault(u => u.Id == id);
            if (usermodel == null)
            {
                return HttpNotFound();
            }
            return View(usermodel);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(UsersSet usermodel)
        {
            if (ModelState.IsValid)
            {
                db.UsersSets.AddObject(usermodel);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(usermodel);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UsersSet usermodel = db.UsersSets.FirstOrDefault(u => u.Id == id);
            if (usermodel == null)
            {
                return HttpNotFound();
            }
            return View(usermodel);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(UsersSet usermodel)
        {
            return HttpNotFound();

 //           if (ModelState.IsValid)
  //          {
//                db.UsersSets.Entry(usermodel).State = EntityState.Modified;

//                db.SaveChanges();
          //      return RedirectToAction("Index");
          //  }
         //   return View(usermodel);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id = 0)
        {
            UsersSet usermodel = db.UsersSets.FirstOrDefault(u => u.Id == id);
            if (usermodel == null)
            {
                return HttpNotFound();
            }
            return View(usermodel);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            UsersSet usermodel = db.UsersSets.FirstOrDefault(u => u.Id == id);

            db.UsersSets.DeleteObject(usermodel);

            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult SearchIndex(string selectField, string selectCondition, string searchString)
        {
            var selectFieldList = new List<string>();
            selectFieldList.Add("All columns");
            selectFieldList.Add("User Name");
            selectFieldList.Add("First Name");
            selectFieldList.Add("Last Name");

            var selectConditionList = new List<string>();
            selectConditionList.Add("eqals");
            selectConditionList.Add("not equal to");
            selectConditionList.Add("starts with");
            selectConditionList.Add("contains");
            selectConditionList.Add("does not contain");
            

            ViewBag.SelectField = new SelectList(selectFieldList);
            ViewBag.SelectCondition = new SelectList(selectConditionList);
            
            var user = from u in db.UsersSets
                         select u;

            if (!String.IsNullOrEmpty(searchString))
            {
               user  = user.Where(s => s.login.Contains(searchString));
            }

            if (string.IsNullOrEmpty(selectField))
                return View(user);
            else
            return View(user.Where(x => x.name == selectField));
        }
    }
}